package com.llt.springcloud.controller;

import com.llt.springcloud.pojo.Dept;
import com.llt.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @Autowired
    //得到具体微服务
    private DiscoveryClient discoveryClient;
    @PostMapping("/dept/add")
    public  boolean addDept(@RequestBody Dept dept){
        System.out.println(dept.getDname());
        return deptService.addDept(dept);
    }
    @GetMapping("/dept/getById/{id}")
    public Dept getDeptById(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }
    @GetMapping("/dept/list")
    public List<Dept> getDeptAll(){
        return deptService.queryAll();
    }
    @GetMapping("/dept/discovery")
    public Object discovery(){
        //获取微服务列表清单
        List<String> services = discoveryClient.getServices();
        System.out.println("discovery:"+services);
        //得到具体微服务
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost());
            System.out.println(instance.getPort());
            System.out.println(instance.getServiceId());
        }
        return this.discoveryClient;
    }
}
